<style>
    .tools-btn { padding: 5px 7px; border-radius: 12px; }
    .type-info, .attach-info { display: none; }

    .range-input { text-align: center; }
    .description { color: #999; font-size: 13px; }
    .send-item-list li, .send-pet-list li { margin: 8px 0; }
    .send-item-list li .form-control, .send-pet-list li .form-control { margin-right: 2px; }
    .chosen-container-single .chosen-single { height: 35px; line-height: 35px; padding: 0 0 0 13px; }
    .chosen-container-single .chosen-single div b { background-position: 0 8px; }
    .chosen-container-active.chosen-with-drop .chosen-single div b { background-position: -18px 8px; }
</style>

<form method="post" action="/oss/?r=notice/doAdd">
    <{include file='../common_params.tpl'}>

    <div class="box box-black">
        <div class="box-body">
            <h5> <i class="fa fa-users"></i>&nbsp;&nbsp;收件人设置</h5>
            <table class="table table-bordered">
                <colgroup>
                    <col style="width: 20%;"></col>
                    <col style="width: 80%;"></col>
                </colgroup>

                <!--<tr>
                    <th class="th-label">服务器</th>
                    <td>
                        <select class="form-control" name="server">
                            <{foreach from=$serverList item=item key=key}>
                        		<option value="<{$key}>-<{$item}>"><{$item}></option>
                        	<{/foreach}>
                        </select>
                    </td>
                </tr>-->
                
                <tr>
                    <th class="th-label">发送类型</th>
                    <td>
                        <select name="send_type" id="sel_send_type" class="form-control">
                            <{html_options options=$sendType}>
                        </select>
                    </td>
                </tr>

                <!--<tr>
                    <th class="th-label">
                        <label>在线玩家 <input type="checkbox" class="simple" value="1" name="cond_1" /></label>
                    </th>
                    <td class="th-label">

                    </td>
                </tr>-->
<!--
                <tr>
                    <th class="th-label">
                        <label>等级范围 <input type="checkbox" class="simple" value="1" name="cond_2" /></label>
                    </th>
                    <td><input type="text" class="form-control inline range-input" name="min_level" placeholder="最低等级" /> ~ <input type="text" class="form-control inline range-input" name="max_level" placeholder="最高等级" /></td>
                </tr>

                <tr>
                    <th class="th-label">
                        <label>注册时间 <input type="checkbox" class="simple" value="3" name="cond_3" /></label>
                    </th>
                    <td><input type="text" class="form-control inline range-input time" name="min_regtime" placeholder="开始时间" /> ~ <input type="text" class="form-control inline range-input time" name="max_regtime" placeholder="结束时间" /></td>
                </tr>

                <tr>
                    <th class="th-label">
                        <label>上次登陆时间 <input type="checkbox" class="simple" value="4" name="cond_4" /></label>
                    </th>
                    <td><input type="text" class="form-control inline range-input time" name="min_logintime" placeholder="开始时间" /> ~ <input type="text" class="form-control inline range-input time" name="max_logintime" placeholder="结束时间" /></td>
                </tr>
-->
            </table>
            <h5> <i class="fa fa-envelope-o"></i>&nbsp;&nbsp;邮件内容</h5>
            <table class="table table-bordered">
                <colgroup>
                    <col style="width: 20%;"></col>
                    <col style="width: 80%;"></col>
                </colgroup>

                <tr>
                    <th class="th-label">邮件标题</th>
                    <td><input type="text" class="form-control" name="title" /></td>
                </tr>

                <tr>
                    <th class="th-label">邮件内容</th>
                    <td><textarea class="form-control" name="content" rows="5"></textarea></td>
                </tr>

                <!--
                <tr>
                    <th class="th-label">邮件附件</th>
                    <td>
                        <label><input type="checkbox" class="attach-check simple" data-val="1" /> 道具</label>
                        <br/>
                        <label><input type="checkbox" class="attach-check simple" data-val="2" /> 宠物</label>
                    </td>
                </tr>

                <tr class="attach-info" for="1">
                    <th class="th-label">发送道具</th>
                    <td>
                        <ul class="nav send-item-list">
                            <li>

                                <select style="width: 140px;" class="sel-item-type form-control inline">
                                    <option value="">--道具类型--</option>
                                    <{html_options options=$itemType}>
                                </select>

                                <select name="item_type[]" style="width: 280px;" class="sel-item-list form-control inline">
                                    <option value="">--请先选择道具类型--</option>
                                </select>

                                <input name="item_id[]" class="form-control inline" placeholder="道具ID" />

                                <input type="text" name="item_number[]" class="form-control inline" placeholder="数量" size="12" />

                                <div class="box-tools inline">
                                    <a href="javascript:;" class="tools-btn tools-btn-add badge bg-green"><i class="fa fa-plus"></i></a>
                                    <a href="javascript:;" class="tools-btn btn-del badge bg-red"><i class="fa fa-minus"></i></a>
                                </div>
                            </li>
                        </ul>

                        <span class="description">道具ID/数量为必填,有一项为空表示不发送道具.</span>
                    </td>
                </tr>

                <tr class="attach-info" for="2">
                    <th class="th-label">发送宠物</th>
                    <td>
                        <input name="pet_id" class="form-control" placeholder="宠物ID" />
                        <span class="description">填写宠物ID时表示发送宠物</span>

                        <p style="margin: 20px 0 0 0; font-size: 13px;">宠物扩展参数(没有则置空):</p>
                        <ul class="nav send-pet-list">
                            <li>
                                <input name="pet_extra_key[]" class="form-control inline" placeholder='Key值' />
                                <input name="pet_extra_val[]" class="form-control inline" placeholder='Value值' />

                                <div class="box-tools inline">
                                    <a href="javascript:;" class="tools-btn pet-btn-add badge bg-green"><i class="fa fa-plus"></i></a>
                                    <a href="javascript:;" class="tools-btn btn-del badge bg-red"><i class="fa fa-minus"></i></a>
                                </div>
                            </li>
                        </ul>

                        <span class="description">需要填写扩展值时,一行内Key值与Value值一一对应.</span>
                    </td>
                </tr>
                -->
<!--
                <tr>
                    <th class="th-label">邮件生效时间</th>
                    <td>
                        <input type="text" class="form-control inline time range-input" name="start_time" placeholder="开始时间" /> ~ <input type="text" class="form-control time inline range-input" name="end_time" placeholder="结束时间" />
                    </td>
                </tr>
-->
            </table>
        </div>

        <div class="box-footer">
        	<a href="javascript:history.go(-1);" class="btn btn-default">返回</a>
            <button type="submit" name="sub" class="btn btn-default pull-right"><i class="fa fa-paper-plane-o"></i> 保存</button>
            <div style="clear: both;"></div>
        </div>
    </div>
</form>

<script type="text/javascript">
    $(function(){
        var itemUrl = '<{$itemUrl nofilter}>';
        var cloneItem = $(".send-item-list li:first-child").clone();
        var clonePet = $(".send-pet-list li:first-child").clone();

        $(".sel-item-list").chosen();
        $(".sel-item-type").live('change', function(){
            var type = $(this).val(),
                    itemSelect = $(this).next('.sel-item-list');

            itemSelect.val('').find('option.item').remove();
            itemSelect.trigger("chosen:updated");
            if(type){
                $.getJSON(itemUrl, { 'type':type }, function(rs){
                    for(var i in rs){
                        itemSelect.append('<option class="item" value="'+rs[i]['id']+'">'+rs[i]['id']+' - '+rs[i]['name']+'</option>');
                    }
                    itemSelect.trigger("chosen:updated");
                });
            }
        });

        $("#sel_send_type").change(function(){
            var sendType = $(this).val();

            $(".type-info").hide().filter('[for="'+sendType+'"]').show();
        }).change();

        $(".attach-check").click(function(){
            var value = $(this).attr('data-val');

            var infoTr = $(".attach-info").filter('[for="'+value+'"]');

            if($(this).is(':checked')){
                infoTr.show();
            }else{
                infoTr.hide();
            }
        });

        $("input.time").datetimepicker({
            format: 'yyyy-mm-dd hh:ii:00',
        });

        $(".range-input").change(function(){
            var object = $(this),
                    index = object.index();

            if(index == 1){
                var from = getRangeVal(object.prev());
                var to = getRangeVal(object);
            }else {
                var from = getRangeVal(object);
                var to = getRangeVal(object.next());
            }

            if(to && from && to < from){
                alert('范围填写错误!');
                object.val('');
            }
        });

        $(".tools-btn-add").live('click',function(){
            var li = $(this).closest('li');
            var oneClone = cloneItem.clone();
            oneClone.insertAfter(li);
            oneClone.find('.sel-item-list').chosen();
        });

        $(".pet-btn-add").live('click',function(){
            var li = $(this).closest('li');
            var oneClone = clonePet.clone();
            oneClone.insertAfter(li);
        });

        $(".btn-del").live('click',function(){
            if(!window.confirm('是否要删除该条数据?')) return true;
            var li = $(this).closest('li'),
                    ul = li.closest('ul');

            if(ul.children('li').length > 1){
                li.remove();
            }else{
                li.find('select, input').val('').change();
            }
        });
        
        
        $('button[name="sub"]').click(function(){
        	var title = $('input[name="title"]').val();
        	if(title == ''){
        		layer.alert('请填写邮件标题', {icon: 2, skin: 'layui-layer-molv'});
            	return false;
        	}
        	
        	var content = $('textarea[name="content"]').val();
        	if(content == ''){
        		layer.alert('请填写邮件内容', {icon: 2, skin: 'layui-layer-molv'});
            	return false;
        	}
        	
        	var content_l = content.length;
        	if(content_l > 1000){
        		layer.alert('邮件内容不能超过1000字', {icon: 2, skin: 'layui-layer-molv'});
        		return false;
        	}
        });

        function getRangeVal(object){
            if(!object) return null;

            var val = object.val();

            if(object.hasClass('time')){
                return Date.parse(new Date(val));
            }else{
                return val;
            }
        }
        
    });
</script>
